//
//  MM_LoginVC.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/11.
//

#import "MM_LoginVC.h"
#import "MM_LoginHeaderView.h"
#import "MM_LoginCell.h"
#import "M80AttributedLabel.h"
#import "MM_LoginBottomLoginView.h"
#import "MM_HomeManageVC.h"

@interface MM_LoginVC ()<UITableViewDelegate,UITableViewDataSource,M80AttributedLabelDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) MM_LoginHeaderView *headerView;
@property (nonatomic, strong) UIView *fotterView;
@property (nonatomic, strong) UIButton *addPayBtn;
@property (nonatomic, strong) MM_LoginBottomLoginView *bottomView;

@end

@implementation MM_LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleView.hidden = YES;
    
    self.view.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(ScreenW, ScreenH )direction:GradientColorDirectionVertical
                                                startColor:MM_ColorUtil(@"#E7F0FF")
                                                  endColor:MM_ColorUtil(@"#FFFFFF")];
    
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.fotterView;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];

    [self loadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MM_LoginCell *cell = [MM_LoginCell cellWithTableView:tableView];
    cell.backgroundColor = [UIColor clearColor];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)m80AttributedLabel:(M80AttributedLabel *)label
             clickedOnLink:(M80AttachmentModel *)linkData {
    
    NSString *linkStr = linkData.attachmentOther;
    
    if (linkStr.integerValue == 1) {
        [self showStateText:@"用户手册"];
    } else {
        [self showStateText:@"隐私政策"];
    }
    
}

- (void)loadData {
    
    NSArray *imageArr = @[@"phoneUserImage",@"passwordUserImage"];
    NSArray *typeArr = @[@(MM_LoginTypePhone),@(MM_LoginTypePassword)];
    NSArray *placeholderArr = @[@"请输入账号",@"请输入密码"];
    
    for (int i=0; i<imageArr.count; i++) {

        MM_LoginModel *model = [[MM_LoginModel alloc] init];
        model.imageStr = imageArr[i];
        model.loginType = [typeArr[i] integerValue];
        model.placeholderText = placeholderArr[i];
        [self.dataArr addObject:model];
    }

    [self.tableView reloadData];
}

- (void)goLoginData {
    
    [self.view endEditing:YES];
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    
    for (MM_LoginModel *model in self.dataArr) {
        
        if (model.loginType == MM_LoginTypePhone) {
            
            if (!stringIsEmpty(model.inputText)) {
                parmDic[@"user_name"] = model.inputText;
            } else {
                [self showStateText:@"请输入用户名!"];
                return;
            }
        } else if (model.loginType == MM_LoginTypePassword) {
            if (!stringIsEmpty(model.inputText)) {
                parmDic[@"password"] = model.inputText;
            } else {
                [self showStateText:@"请输入密码!"];
                return;
            }
        }
    }
    
    [self showHudInView:self.view hint:@"加载中"];
    [MM_Http postRequestURLStr:KMM_User_Login withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [[MM_Cache shareMM_Cache] saveUserJson:[requestDic mj_JSONString]];
        
        MM_HomeManageVC *manageVC = [[MM_HomeManageVC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:manageVC];
        AppDelegate.appDelegate.window.rootViewController = nav;
        NSLog(@"requestDicrequestDicrequestDic == %@",requestDic);
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (UITableView *)tableView {

    if (_tableView == nil) {

        _tableView = [UITableView estimatedLayoutTableview:CGRectMake(0, 0, ScreenW, ScreenH) tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _tableView.backgroundColor = [UIColor clearColor];
//        if (@available(iOS 11.0, *)) {
//            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        } else {
//            self.automaticallyAdjustsScrollViewInsets = NO;
//        }
    }
    return _tableView;
}

- (MM_LoginHeaderView *)headerView {

    if (_headerView == nil) {

        _headerView = [[MM_LoginHeaderView alloc] initWithFrame:CGRectZero];
    }
    return _headerView;
}

- (UIView *)fotterView {
    if (!_fotterView) {
        _fotterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 150)];
        
        _addPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addPayBtn setImage:[UIImage imageNamed:@"login_un_selected"] forState:UIControlStateNormal];
        [_addPayBtn setImage:[UIImage imageNamed:@"login_selected"] forState:UIControlStateSelected];
        _addPayBtn.selected = YES;
        [_addPayBtn addTarget:self action:@selector(iconBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _addPayBtn.frame = CGRectMake(32, 0, 40, 40);
        _addPayBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        [_fotterView addSubview:_addPayBtn];
        
        M80AttributedLabel *iconDescLabel = [[M80AttributedLabel alloc]initWithFrame:CGRectMake(self.addPayBtn.MM_Max_X, 10, 280, 20)];
        iconDescLabel.numberOfLines = 0;
        iconDescLabel.font = FONT(12);
        iconDescLabel.textColor = [ColorUtil colorWithHexadecimal:@"#999999"];
        iconDescLabel.linkColor = [ColorUtil colorWithHexadecimal:@"#999999"];
        iconDescLabel.backgroundColor = [UIColor clearColor];
        iconDescLabel.type = M80AttachmentTypeNone | M80AttachmentTypeNormal|M80AttachmentTypeUserName |M80AttachmentTypeURL|M80AttachmentTypeEmotion;
        iconDescLabel.delegate = self;
        iconDescLabel.lineSpacing = 10;
        NSString *descName = [NSString stringWithFormat:@"[[%@||||%@]]",@"用户手册",@"1"];
        NSString *replyName = [NSString stringWithFormat:@"[[%@||||%@]]",@"隐私政策",@"2"];
        iconDescLabel.m80Text = [NSString stringWithFormat:@"我已阅读%@和%@",descName,replyName];
        [_fotterView addSubview:iconDescLabel];
        
        UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [loginBtn MM_ConfigButtonWithTitle:@"立即登录" titleFont:FONT_BOLD(16) titleColor:MM_ColorUtil(@"#FFFFFF") bgColor:MM_ColorUtil(@"#1F71C4") cornerRadius:22 borderColor:nil borderWidth:0];
        loginBtn.frame = CGRectMake(37, self.addPayBtn.MM_Max_Y + 34, ScreenW - 37 - 37, 44);
        [loginBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_fotterView addSubview:loginBtn];
        
        
    }
    return _fotterView;
}

- (MM_LoginBottomLoginView *)bottomView {
    
    if (_bottomView == nil) {
        
        _bottomView = [[MM_LoginBottomLoginView alloc] initWithFrame:CGRectMake(0, ScreenH - 30 - 120, ScreenW, 80)];
    }
    return _bottomView;
}

- (NSMutableArray *)dataArr {

    if (_dataArr == nil) {

        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (void)iconBtnClick:(UIButton *)button {
    button.selected = !button.selected;
    
}

- (void)btnClick:(UIButton *)button {
    [self goLoginData];
}

@end
